/**
* Created by Administrator on 15-9-25.
*/
var fabuzheid=0;
var globalbmnum=0;
var maxbmnum=0;
function jumptocomment(){
    var id=$.getUrlParam("id");

    location.href='xiedianping.html?id='+id+'&param=tongyou';
}
window.onload=function(){
    var id=$.getUrlParam("id");
    var userId=getUserId();
    var samewayId = $.getUrlParam('id')
    $("#jubao").attr('href','jubao.html?objId='+id+"&type=1");

    //详细行程
    var url = getUrl();
    $.jsonp({
        url: 'http://'+url+'/api/sameway/detail-'+id,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions){
            fabuzheid=data.user.id;
            $("#phone").attr('onclick','easeMobGoChat('+data.user.telphone+')');
            var nickName = data.user.nickName;
            $("#nickName").text(nickName);
            var sex=data.user.sex;
            if(data.user.sex==1){
                $("#sexicon").addClass("icons-sex-male");
                $("#sex").addClass("sex sex-male");
                //icons-sex-female
            }else{
                $("#sexicon").addClass("icons-sex-female");
                $("#sex").addClass("sex sex-female");
            }
            var age = data.user.age;
            $("#age").text(age);
            var photo = data.user.photo;
            $("#photo").attr('src',photo);
            var charisma = data.user.charisma;
            $("#charisma").text(charisma);
            var startTime = data.sameway.startTime;

            $("#startTime").text(getTime(data.sameway.startTime));
            var endTime = data.sameway.endTime;

            $("#endTime").text(getTime(data.sameway.endTime));
            var wayType = data.sameway.wayType;
            $("#wayType").text(wayType);
            var goAddress = data.sameway.goAddress;
            $("#goAddress").text(goAddress);
            var targetSpots = data.sameway.targetSpots;
            $("#targetSpots").text(targetSpots);
            var rentType = data.sameway.rentType;
            $("#rentType").text(rentType);
            var wayNumber = data.sameway.wayNumber;
            maxbmnum=wayNumber;
            $("#wayNumber").text(wayNumber);
            var contact = data.sameway.contact;
            $("#contact").text(contact);
            var brightSpot = data.sameway.brightSpot;
            $("#brightSpot").text(brightSpot);
            var wayDesc = data.sameway.wayDesc;
            $("#wayDesc").text(wayDesc);
            var photos = data.sameway.photos[0];
            $(".photos").attr('src',photos);
            $(".edit").attr('data-gz',data.user.id);
            $(".log-bm").attr('data-bm',id);
            $(".log-sc").attr('data-sc',samewayId);


            //判断是否关注
            var tadeuserid=data.user.id;
            $.jsonp({
                url: 'http://'+url+'/api/user/user_cluck_list',
                data: {userId:userId},
                callbackParameter: "callback",
                success: function (data,textStatus,xOptions) {

                    $.each(data.arrays,function(index,ele){

                        if(ele.cluckId==tadeuserid){
                            $(".edit").text("已关注").css({'color': '#c5c5c5','border':'1px solid #c5c5c5'});
                        }
                    });
                    $(".edit").show();
                },
                error: function (xOptions, textStatus) {
                }

            });
            //判断是否报名
//            $.jsonp({
//                url: 'http://'+url+'/api/sameway/assist_sameway-'+userId,
//                data: {userId:userId},
//                callbackParameter: "callback",
//                success: function (data,textStatus,xOptions) {
//                    $.each(data.arrays,function(index,ele){
//                        if(ele.id==id){
//                            $(".log-bm").text("已报名")
//
//                        }
//                    });
//                    $("#bottomul").show();
//                },
//                error: function (xOptions, textStatus) {
//                }
//
//            });
            //判断是否收藏
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_sameway_list',
                data: {userId:userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        //var id = ele.samewayId;
                        if(ele.samewayId==id){
                            $(".log-sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    });
                    $("#boot").show();
                },
                error: function (xOptions, textStatus) {}
            });
        },

        error: function (xOptions, textStatus) {
        }
    });
    //添加查看人数
    $.jsonp({
        url: 'http://'+url+'/api/sameway/look_sameway-'+id,
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
        },

        error: function (xOptions, textStatus) {
        }
    });
    //看过的人
    $.jsonp({
        url: 'http://'+url+'/api/sameway/look_number-'+id,
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#lookNumber").text(data.lookNumber);

            $.each(data.arrays,function(index,ele){
                var sex="female";
                if(ele.sex==1)
                    sex="male";
                var html='<ul class="order-list">' +
                    '<li>' +
                    '<div class="order-list-avatar">' +
                    '<img src="'+ele.photo+'"alt="">' +
                    '</div>' +
                    '<div class="order-list-intro">' +
                    '<div class="order-list-intro-text">' +
                    '<p class="color666 fz16">'+ele.nickName+'</p>' +
                    '<p class="color666 fz14">' +
                    '<span class="sex sex-'+sex+' no-left">' +
                    '<i class="icons-sex-'+sex+'"></i>'+ele.age+'</span>' +
                    '</p>' +
                    '</div>' +
                    '</div>' +
                    ' </li>' +
                    '</ul>';
                $(".ma_con02 ").append(html)
            })
        },

        error: function (xOptions, textStatus) {
        }
    });
    var id=$.getUrlParam("id");
    //评论过的人
    $.jsonp({
        url: 'http://'+url+'/api/sameway/comment_list-'+id,
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#comentNumber").text(data.comentNumber);
            $.each(data.arrays,function(index,ele){
                (function (){
                    var n = new Date().getTime();
                    o = n-ele.createTime*1000;
                    ele.createTime = ago(o);//返回距今时间
                    //传入时间毫秒 如果是时间戳 *1000
                })();

                var sex="female";
                if(ele.sex==1)
                    sex="male";
                var html='<ul class="comment-list">' +
                    '<li class="ui-border-b">' +
                    '<div class="comment-list-avatar">' +
                    '<img src="'+ele.photo+'"width="40" height="40" alt="">' +
                    '</div>' +
                    '<div class="comment-list-content">' +
                    '<div class="comment-list-content-text">' +
                    '<div class="clearfix"><div class="comment-list-content-user">'+ele.nickName+'' +
                    '<span class="sex sex-'+sex+' no-left"><i class="icons-sex-'+sex+'"></i>'+ele.age+'</span>' +
                    '</div>' +
                    '<div class="comment-list-content-time">' +
                    '<p>'+ele.createTime+'</p>' +
                    '</div>' +
                    '</div>' +
                    '<p class="color666 fz12">'+ele.comment+'</p>' +
                    '</div>' +
                    '</div>' +
                    '</li>' +
                    '</ul>';
                $(".ma_con03").append(html)

            })
        },

        error: function (xOptions, textStatus) {
        }
    });
    var id=$.getUrlParam("id");
    //已报名的人数
    $.jsonp({
        url: 'http://'+url+'/api/sameway/assist_sameway-'+userId,
        data: {userId:userId},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            //报名人数
            //console.log(data)

            $("#enterIdsNumber").text(data.arrays.length);
            globalbmnum=data.arrays.length;
            $.each(data.arrays,function(index,ele){
                if(ele.id==id){
                  $(".log-bm").text("已报名")
                }
                $("#bottomul").show();
                var sex="female";
                if(ele.sex==1)
                    sex="male";
                var html='<ul class="order-list order-list-grey">'+
                    '<li>' +
                    '<div class="order-list-avatar">' +
                    '<img src="'+ele.photo+'" alt="">' +
                    '</div>' +
                    '<div class="order-list-intro">' +
                    '<div class="order-list-intro-text">' +
                    '<p class="color666 fz16">'+ele.nickName+'</p>' +
                    '<p class="color666 fz14"><span class="sex sex-'+sex+' no-left"><i class="icons-sex-'+sex+'"></i>'+ele.age+'</span></p>' +
                    '</div>' +
                    '</div>' +
                    ' </li>' +
                    '</ul>';
                $(".ma_con04").append(html)
            })
        },

        error: function (xOptions, textStatus){
        }
    });

    $(document).on('click','.edit',function(){
        var tadeuserid=$(this).data('gz');
        var _txt=$(this).text();
        var obtn=this;
        if (userId==fabuzheid)
            showToast("不能关注自己的同游~");
            $.jsonp({
                url: 'http://'+url+'/api/user/cluck_user-'+tadeuserid,
                data: {userId:userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {

                    var code = data.code;
                    if(code==1) {
                        $(obtn).removeClass("btn").text("已关注").css({color: '#c5c5c5', 'border': '1px solid #c5c5c5'});
                        showToast("关注成功");

                    }else{
                        $(obtn).addClass("btn").text("+关注").css({color: '#00a5e0', width: '60px', height: '30px', 'padding':'0','border':'1px solid #00a5e0'});
                        showToast("取消关注成功")
                    }

                },
                error: function (xOptions, textStatus) {
                }
            });
    })

    $(document).on('click','.log-bm',function(){
        if (userId==fabuzheid){
            showToast("不能报名自己的同游~");
            return;
        }
        if(globalbmnum==maxbmnum){
            showToast("报名人数已满~");
            return;
        }
        var id=$(this).data('bm');
        var _txt=$(this).text();
        var obtn=this;
        $.jsonp({
            url: 'http://'+url+'/api/sameway/enter-' + id,
            data: {userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {

                var code = data.code;
                if(code==1) {
                    $(obtn).addClass("album-bottom-menu-apply").text("报名")
                    showToast("取消报名成功~");
                }else{
                    $(obtn).addClass("album-bottom-menu-apply").text("已报名")
                    showToast("报名成功~");
                }

            },

            error: function (xOptions, textStatus) {
            }
        });
    });
    //收藏
    $(document).on('click','.w_shac',function(){
        if (userId==fabuzheid){
            showToast("不能收藏自己的同游~");
            return;
        }
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_sameway',
            data: {samewayId:id,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".log-sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("取消收藏成功")
                }else{
                    $(".log-sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("收藏成功")
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}

//function onBackKeyDown() {
//    jumpbyback(1,"");
//}
function ago(o){
    var times =o;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var year = month * 365;
    var yearC = times/year;
    var monthC =times/month;
    var dayC =times/day;
    var hourC =times/hour;
    var minC =times/minute;
    var html ="";
    if(yearC>=1){
        html += parseInt(yearC) + "年前";
        return html;
    }else if(monthC>=1){
        html += parseInt(monthC) + "月前";
        return html;
    }else if(dayC>=1){
        html += parseInt(dayC) + "天前";
        return html;
    }else if(hourC>=1){
        html += parseInt(hourC) + "个小时前";
        return html;
    }else if(minC>=1){
        html += parseInt(minC) + "分钟前";
        return html;
    }else{
        return "刚刚";
    }
}

function getTime(t){
    var nt = t*1000;
    var n = new Date(nt);
    return formatDate(n);
}
function formatDate(now)   {
    var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    var   date=now.getDate();
    return   year+"-"+(month>=10?month:"0"+month)+"-"+(date>=10?date:"0"+date);
}